<?php

declare (strict_types=1);

namespace app\model;


use app\BaseModel;
use think\model\concern\SoftDelete;

use function myException;

class SystemConfigModel extends BaseModel
{
    use SoftDelete;

    protected $autoWriteTimestamp = true;
    //软删除
    protected $deleteTime = 'delete_time';
    protected $hidden = ['delete_time', 'update_time'];
    protected $name = 'system_config';


    public function uploadConfig($data): array
    {
        $search = [
            '{%default%}',
            '{%image_size%}',
            '{%image_ext%}',
            '{%image_mime%}',
            '{%video_size%}',
            '{%video_ext%}',
            '{%video_mime%}',
            '{%file_size%}',
            '{%file_ext%}',
            '{%file_mime%}',
            '{%aliyun_access_id%}',
            '{%aliyun_access_secret%}',
            '{%aliyun_bucket%}',
            '{%aliyun_endpoint%}',
            '{%aliyun_isCName%}',
            '{%aliyun_cdnUrl%}',
            '{%aliyun_prefix%}',
            '{%qiniu_access_key%}',
            '{%qiniu_secret_key%}',
            '{%qiniu_bucket%}',
            '{%qiniu_domain%}',
            '{%qcloud_region%}',
            '{%qcloud_app_id%}',
            '{%qcloud_secret_id%}',
            '{%qcloud_secret_key%}',
            '{%qcloud_bucket%}',
            '{%qcloud_timeout%}',
            '{%qcloud_connect_timeout%}',
            '{%qcloud_cdn%}',
            '{%qcloud_scheme%}',
            '{%qcloud_read_from_cdn%}',
        ];
        // 定义变量名与 sign 的映射关系
        $signToVarMap = [
            'upload_mode' => 'default',
            'image_upload_max' => 'image_size',
            'image_ext' => 'image_ext',
            'image_mime' => 'image_mime',
            'video_upload_max' => 'video_size',
            'video_ext' => 'video_ext',
            'video_mime' => 'video_mime',
            'file_upload_max' => 'file_size',
            'file_ext' => 'file_ext',
            'file_mime' => 'file_mime',
            'aliyun_access_id' => 'aliyun_access_id',
            'aliyun_access_secret' => 'aliyun_access_secret',
            'aliyun_bucket' => 'aliyun_bucket',
            'aliyun_endpoint' => 'aliyun_endpoint',
            'aliyun_isCName' => 'aliyun_isCName',
            'aliyun_cdnUrl' => 'aliyun_cdnUrl',
            'aliyun_prefix' => 'aliyun_prefix',
            'qiniu_access_key' => 'qiniu_access_key',
            'qiniu_secret_key' => 'qiniu_secret_key',
            'qiniu_bucket' => 'qiniu_bucket',
            'qiniu_domain' => 'qiniu_domain',
            'tx_region' => 'qcloud_region',
            'tx_app_id' => 'qcloud_app_id',
            'tx_secret_id' => 'qcloud_secret_id',
            'tx_secret_key' => 'qcloud_secret_key',
            'tx_bucket' => 'qcloud_bucket',
            'tx_timeout' => 'qcloud_timeout',
            'tx_connect_timeout' => 'qcloud_connect_timeout',
            'tx_cdn' => 'qcloud_cdn',
            'tx_scheme' => 'qcloud_scheme',
            'tx_read_from_cdn' => 'qcloud_read_from_cdn',
        ];

        $result = array_fill_keys(array_values($signToVarMap), '');

        foreach ($data as $item) {
            if (isset($signToVarMap[$item['sign']])) {
                $result[$signToVarMap[$item['sign']]] = $item['value'];
            }
        }
        $result['aliyun_isCName'] = $result['aliyun_isCName'] ? 'true' : 'false';
        $result['qcloud_read_from_cdn'] = $result['qcloud_read_from_cdn'] ? 'true' : 'false';
        return [$search,$result];
    }
}